package com.locust.upms.web.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.locust.base.common.base.web.support.BaseResult;
import com.locust.base.common.base.web.support.controller.BaseController;
import com.locust.base.common.utils.Paginator;
import com.locust.upms.authc.annotaion.RequiresPermissions;
import com.locust.upms.rpc.api.UpmsCompanyService;
import com.locust.upms.rpc.entity.UpmsCompany;

@RestController
@RequestMapping(value = "/api/upms/company")
public class UpmsCompanyController extends BaseController {

	@Autowired
	private UpmsCompanyService upmsCompanyService;

	@RequiresPermissions("upms:company:add")
	@PostMapping(value = "/add")
	public Object add(@RequestBody UpmsCompany entity) {
		if (StringUtils.isBlank(entity.getName())) {
			return BaseResult.failure("名称不能为空");
		}
		if (null != this.upmsCompanyService.checkValue("name", entity.getName())) {
			return BaseResult.failure("名称已存在");
		}
		if (StringUtils.isBlank(entity.getCode())) {
			return BaseResult.failure("编码不能为空");
		}
		if (null != this.upmsCompanyService.checkValue("code", entity.getCode())) {
			return BaseResult.failure("编码已存在");
		}
		this.upmsCompanyService.insertSelective(entity);

		return BaseResult.ok();
	}

	@RequiresPermissions("upms:company:delete")
	@GetMapping(value = "/delete/{id}")
	public Object delete(@PathVariable Integer id) {
		this.upmsCompanyService.delete(id);
		return BaseResult.ok();
	}

	@RequiresPermissions("upms:company:update")
	@PostMapping(value = "/update")
	public Object update(@RequestBody UpmsCompany entity) {
		if (StringUtils.isBlank(entity.getName())) {
			return BaseResult.failure("名称号码不能为空");
		}
		if (StringUtils.isBlank(entity.getCode())) {
			return BaseResult.failure("编码不能为空");
		}
		UpmsCompany oldMenu = this.upmsCompanyService.selectById(entity.getId()) ;
		if(null != oldMenu.getName() && !oldMenu.getName().equals(entity.getName())) {
			if(null != this.upmsCompanyService.checkValue("name", entity.getName())) {
				return BaseResult.failure("名称已存在");
			}
		}
		if(null != oldMenu.getCode() && !oldMenu.getCode().equals(entity.getCode())) {
			if(null != this.upmsCompanyService.checkValue("code", entity.getCode())) {
				return BaseResult.failure("编码已存在");
			}
		}
		this.upmsCompanyService.update(entity);

		return BaseResult.ok();
	}

	@RequiresPermissions("upms:company:detail")
	@GetMapping(value = "/get/{id}")
	public Object list(@PathVariable Integer id) {
		return this.upmsCompanyService.selectById(id);
	}
	
	@RequiresPermissions("upms:company:list")
	@GetMapping(value = "/list")
	public Object list(UpmsCompany entity) {
		return this.upmsCompanyService.selectPage(new Paginator(this.request), entity);
	}

	@RequiresPermissions("upms:company:tree")
	@GetMapping(value = "/tree")
	public Object tree(UpmsCompany entity) {
		return this.upmsCompanyService.tree(entity);
	}
	
	/**
	 * 移除该公司下的所有用户
	 * @param companyId
	 * @return
	 */
	@RequiresPermissions("upms:company:remove_user_assign")
	@GetMapping(value = "/remove_company_user")
	public Object removeCompanyAssignAllUser(Integer companyId) {
		this.upmsCompanyService.removeCompanyAssignAllUser(companyId);
		return BaseResult.ok();
	}
	
}
